<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Modules\Blog\Service;


use App\Models\Blog\Link;
use Illuminate\Http\Request;

class LinkService
{

    use Service;

    /**
     * 获取友情链接
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function LinkIndexService( $request )
    {
        $user   = $this->getCommon()->UserCommonService();
        $where  = function ( $query ) use ( $request, $user ) {
            $query->where('status', 1);
            $query->where('user_id', $user->id);
            if ( $request->filled('is_show') ) $query->where('is_show', $request->input('is_show'));
            if ( $request->filled('name') ) $query->where('name', 'like', "%" . $request->input('name') . "%");
            if ( $request->filled('created_at') ) {
                $time       = $request->input('created_at');
                $start_time = $time[ 0 ];
                $end_time   = $time[ 1 ];
                $query->whereBetween('created_at', [ $start_time, $end_time ]);
            }
        };
        $select = [ 'id', 'name', 'user_id', 'is_show', 'content', 'status', 'url', 'pic', 'updated_at' ];
        $with   = [
            'file' => function ( $query ) {
                $query->select('id', 'url');
            },
        ];
        $link   = Link::GetModel($where, $with, $select, $request->input('limit'));
        return data($link);
    }


    /**
     * 新增链接
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function LinkCreateService( $request )
    {
        $user      = $this->getCommon()->UserCommonService();
        $validator = $this->getBlogValidator()->BlogLinkValidator($request);
        if ( $validator->first() ) return message($validator->first());
        $data[ 'name' ]    = $request->input('name');
        $data[ 'is_show' ] = $request->input('is_show') == TRUE ? 1 : 2;
        $data[ 'url' ]     = $request->input('url');
        $data[ 'pic' ]     = $request->input('pic');
        $data[ 'content' ] = $request->input('content');
        $data[ 'user_id' ] = $user->id;
        Link::CreateModel($data);
        return data(NULL, $this->getErrorCode()::message(209));
    }


    /**
     * 修改链接状态
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function LinkIsShowService( $request )
    {
        $user          = $this->getCommon()->UserCommonService();
        $where         = function ( $query ) use ( $request, $user ) {
            $query->where('id', $request->input('id'));
            $query->where('user_id', $user->id);
        };
        $link          = Link::FirstModel($where, [], [ 'id', 'user_id', 'is_show' ]);
        $link->is_show = $request->input('is_show') == 1 ? 1 : 2;
        $link->save();
        return data(NULL, $this->getErrorCode()::message(203));
    }

    /**
     * 删除链接
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function LinkDestroyService( $id )
    {
        $user  = $this->getCommon()->UserCommonService();
        $where = function ( $query ) use ( $id, $user ) {
            if ( ! is_array($id) ) $query->where('id', $id);
            else $query->whereIn('id', $id);
            $query->where('user_id', $user->id);
        };
        Link::DeleteModel($where);
        return data(NULL, $this->getErrorCode()::message(204));
    }

    /**
     * 获取链接详情
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function LinkShowService( $id )
    {
        $user   = $this->getCommon()->UserCommonService();
        $where  = function ( $query ) use ( $id, $user ) {
            $query->where('user_id', $user->id);
            $query->where('id', $id);
        };
        $with   = [
            'file' => function ( $query ) {
                $query->select('id', 'url');
            },
        ];
        $select = [ 'id', 'name', 'user_id', 'is_show', 'content', 'status', 'url', 'pic', 'updated_at' ];
        $link   = Link::FirstModel($where, $with, $select);
        return data($link);
    }

    /**
     * 更新 链接
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function LinkUpdateService( $request )
    {
        $user      = $this->getCommon()->UserCommonService();
        $validator = $this->getBlogValidator()->BloglinkValidator($request);
        if ( $validator->first() ) return message($validator->first());
        $where             = function ( $query ) use ( $request, $user ) {
            $query->where('id', $request->input('id'));
            $query->where('user_id', $user->id);
        };
        $data[ 'name' ]    = $request->input('name');
        $data[ 'is_show' ] = $request->input('is_show') == TRUE ? 1 : 2;
        $data[ 'url' ]     = $request->input('url');
        $data[ 'pic' ]     = $request->input('pic');
        $data[ 'content' ] = $request->input('content');
        $data[ 'user_id' ] = $user->id;
        Link::UpdateModel($where, $data);
        return data(NULL, $this->getErrorCode()::message(210));
    }
}
